package jmu.edu.cn.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
	private static final DBUtils utils = new DBUtils();

	private static final String CONN_URL = "jdbc:mysql://localhost:3306/device_repair";
	private static final String USER = "root";
	private static final String PWD = "panzer";

	private DBUtils() {

	}

	public static DBUtils getInstance() {
		return utils;
	}

	public Connection getConn() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(CONN_URL, USER, PWD);
		} catch (ClassNotFoundException e) {
			System.out.println("the driver of mysql is not found!");
		} catch (SQLException e) {
			System.out.println("failed to connect!");
		}
		return conn;
	}

	public void releaseAll(Connection conn, PreparedStatement psmt, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (psmt != null)
				psmt.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			System.out.println("failed to disconnect!");
		}
	}
}
